#include "StdAfx.h"
#include "GEdit.h"


GEdit::GEdit(void)
{
	m_nPos = 0;
	GetTextExtentPoint32A(wglGetCurrentDC(), "1", 1, &m_szText);
	m_nStart = 0;
	m_nEnd = 0;
	m_nTimes = 0;
}

GEdit::~GEdit(void)
{
}
void GEdit::LoadResAttritute(map<tstring, tstring>& mapAtt)
{
	map<tstring, tstring>::iterator iter;
	iter = mapAtt.find(_T("bkbmp"));
	if (iter == mapAtt.end()) assert(0);
	m_texture = GImageLoad::LoadTextureFromFile(iter->second.c_str());

	GWnd::LoadResAttritute(mapAtt);
}
void GEdit::MouseClick(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON)
	{
		if (IsPointInWnd(x, y))
		{
			m_bFocus = TRUE;

		}
		else
		{
			m_bFocus = FALSE;
		}
	}
}
void GEdit::ReCalculateText(bool bAdd /* = true */)
{
	int	iEnd = 0;
	SIZE sz;
	for (unsigned int i=0; i<m_strText.length()-m_nStart; i++)
	{
		wstring str = m_strText.substr(m_nStart, i+1);
		//GetTextExtentPoint32(GFont::Inst().GetDC(), str.c_str(), i+1, &sz);
		int iCurOffset = 0;
		for(int i = 0; i < str.length(); i++)
		{
			if((int)(str[i]) >= 0 && (int)(str[i]) <= 255)
				iCurOffset += 6;
			else
				iCurOffset += 12;
		}
		if (/*sz.cx*/iCurOffset <= m_nRight-m_nLeft)
		{
			iEnd++;
		}
		else
		{
			if (bAdd)
			{
				m_nStart++;
				wstring strTem = m_strText.substr(m_nStart, iEnd);
				SIZE sz1;
				//GetTextExtentPoint32(GFont::Inst().GetDC(), strTem.c_str(), strTem.length(), &sz1);
				int iCurOffset1 = 0;
				for(int i = 0; i < strTem.length(); i++)
				{
					if((int)(strTem[i]) >= 0 && (int)(strTem[i]) <= 255)
						iCurOffset1 += 6;
					else
						iCurOffset1 += 12;
				}
				if(/*sz1.cx*/iCurOffset1 > m_nRight - m_nLeft)
					m_nStart++;
			}
			break;
		}
	}
	m_strText = m_strText.substr(m_nStart, iEnd);
}
void GEdit::HandleMessage(UINT uMsg, int wParam, int lParam)
{
	if (m_bFocus)
	{
		if (uMsg == WM_IME_CHAR)
		{
			wchar_t ch= (wchar_t)LOWORD(wParam);
			m_strText.insert(m_nPos++, 1, ch);
			ReCalculateText();					
		}
		else if (WM_KEYDOWN==uMsg)
		{
			char ch = LOWORD(wParam);
			switch(ch)
			{
			case VK_LEFT:
				{
					if (m_nPos>0) m_nPos--;
					if (m_nStart>0)
					{	
						if(m_nPos < m_nStart)
							m_nStart--;	
					}
					ReCalculateText(false);
					DebugString(_T("start=%d,pos = %d\n"), m_nStart, m_nPos);
					break;
				}
			case VK_RIGHT:
				{
					if (m_nPos>=0 && m_nPos < m_strText.length()) m_nPos++;
					if (m_strText.length()<m_nPos-m_nStart)m_nStart++;				
					ReCalculateText(false);
					DebugString(_T("start=%d,pos = %d\n"), m_nStart, m_nPos);
				}
				break;
			case VK_DELETE:
				{
					bool flag = FALSE;
					if(m_nPos < m_strText.length())
					{
						m_strText = m_strText.substr(0,m_nPos) + m_strText.substr(m_nPos+1,m_strText.length()-m_nPos);
						SIZE sz3;
						for(int i = 0; i <= m_strText.length() - m_nStart; i++)
						{
							wstring strTem2 = m_strText.substr(m_nStart,i);
							GetTextExtentPoint32(GFont::Inst().GetDC(), strTem2.c_str(), strTem2.length(), &sz3);
							if(sz3.cx >= m_nRight - m_nLeft || sz3.cx == m_nRight - m_nLeft - 6)
								break;
							else if(i == m_strText.length() - m_nStart)
							{
								flag = TRUE;
							}
							else
							{
								continue;
							}
						}
						if(flag)
						{
							while(m_nStart > 0)
							{
								m_nStart --;
								SIZE sz4;
								wstring strTem3 = m_strText.substr(m_nStart,m_strText.length()-m_nStart);
								GetTextExtentPoint32(GFont::Inst().GetDC(), strTem3.c_str(), strTem3.length(), &sz4);
								if(sz4.cx < m_nRight - m_nLeft)
									continue;
								else
									break;
							}
						}
					}
					ReCalculateText(FALSE);
					break;
				}
			default:
				break;
			}
		}
		else if (WM_CHAR == uMsg)
		{
			char ch = LOWORD(wParam);
			if (isprint(ch)||ch==' ')
			{
				m_strText.insert(m_nPos++, 1, wchar_t(ch));
				ReCalculateText();
			}
			else
			{
				char ch = LOWORD(wParam);
				switch(ch)
				{
				case VK_BACK:
					if(m_nPos != 0)
					{
						m_strText = m_strText.substr(0,m_nPos-1) + m_strText.substr(m_nPos,m_strText.length()-m_nPos);
						SIZE sz2;
						for(int i = m_nStart; i > 0; i--)
						{
							wstring strTem1 = m_strText.substr(m_nStart,m_strText.length()-m_nStart);
							GetTextExtentPoint32(GFont::Inst().GetDC(), strTem1.c_str(), strTem1.length(), &sz2);
							if(sz2.cx > m_nRight - m_nLeft)
								break;
							else
							{
								m_nStart--;
							}
						}
						if(m_nPos > 0)
							m_nPos --;
					}
					ReCalculateText();
					break;
				default:
					break;
				}
			}
		}
	}
}
void GEdit::Draw()
{
	GRECT rc(m_nLeft, m_nTop, m_nRight, m_nBotton);
	rc.Offset(m_nParentLeft, m_nParentBotton);
	glBindTexture(GL_TEXTURE_2D, m_texture);

	glColor4f(1,1,1,1);
	glBegin(GL_QUADS);
	glTexCoord2f( 0, 0);glVertex2f(rc.left, rc.top);  
	glTexCoord2f( 1, 0);glVertex2f(rc.right, rc.top);
	glTexCoord2f( 1, 1);glVertex2f(rc.right, rc.botton);
	glTexCoord2f( 0, 1);glVertex2f(rc.left, rc.botton);	
	glEnd();

	m_font.DrawLine(m_strText.c_str(), GRGB(255, 0, 0), rc.left+1, rc.top+4);

	if (m_bFocus)
	{		
		if (m_nTimes%15 == 0 || (m_nTimes-1)%15 == 0 || (m_nTimes-2)%15 == 0)
		{
			SIZE sz;
			GetTextExtentPoint32(GFont::Inst().GetDC(), m_strText.c_str(),m_nPos-m_nStart, &sz);
			glColor3f(0,0,0);
			glBegin(GL_LINES);
				glVertex2f(rc.left+sz.cx, rc.top);
				glVertex2f(rc.left+sz.cx, rc.botton);
			glEnd();
			glColor3f(1,1,1);
		}
		m_nTimes++;
	}
	else
	{
		m_nTimes = 0;
	}


}